package com.hframe.typefilter;

import org.apache.log4j.Logger;
import sun.misc.BASE64Decoder;

/**
 * Base64解密
 * 
 * @author Administrator
 *
 */
public class Base64Decoder implements FilterRef<String, Object> {
	static Logger log = Logger.getLogger(Base64Decoder.class);

	@Override
	public String filter( Object object) {
		return decoder((String) object);
	}

	public static String decoder(String val) {
		if (val != null && val.length() > 0) {
			BASE64Decoder decoder = new BASE64Decoder();
			try {
				return new String(decoder.decodeBuffer(val), "utf-8");
			} catch (Exception e) {
				log.error(e, e);
			}
		}
		return "";
	}

	public static byte[] decoderToBytes(String val) {
		if (val != null && val.length() > 0) {
			BASE64Decoder decoder = new BASE64Decoder();
			try {
				return decoder.decodeBuffer(val);
			} catch (Exception e) {
				log.error(e, e);
			}
		}
		return null;
	}
}